import { API } from '../api'
import { CydonElement, define } from 'cydon'
import { error, success } from '@cydon/ui/Message'
import { EditableTable } from './EditableTable'
import { dragText } from '../utils/directives'

/**
 * @element import-tags
 * 导入标签
 */
@define('import-tags')
export class ImportTags extends CydonElement {
	tags = ''

	connectedCallback() {
		this.$directives = [dragText, ...this.$directives]
		super.connectedCallback()
	}

	importTags() {
		API.importTags(this.tags).then(count => {
			success(`成功导入${count}个标签`);
			(<EditableTable>this.previousSibling).update()
		}, error)
	}

	exportTags() {
		API.exportTags().then(data => {
			success('导出成功')
			this.tags = data
		}, error)
	}
}

declare global {
	interface HTMLElementTagNameMap {
		'import-tags': ImportTags
	}
}